package cn.wufuqi.recyclerviewbindingadapter.view

import android.content.Context
import android.os.Build
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.ViewGroup
import android.widget.Toast
import androidx.annotation.RequiresApi
import com.scwang.smartrefresh.layout.SmartRefreshLayout






class ScrollSmartRefreshLayout : SmartRefreshLayout {

    private var mIsOpenScroll = true

    /**
     * 临时的
     */
    private var mIsTemporaryOpenScroll = true

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    )


    fun setIsOpenScroll(isOpenScroll: Boolean) {
        mIsOpenScroll = isOpenScroll
    }


    fun getIsOpenScroll() = mIsOpenScroll


    fun setIsTemporaryOpenScroll(isOpenScroll: Boolean) {
        mIsTemporaryOpenScroll = isOpenScroll
    }


    fun getIsTemporaryOpenScroll() = mIsTemporaryOpenScroll


    override fun dispatchTouchEvent(e: MotionEvent?): Boolean {
        if (e?.action == MotionEvent.ACTION_UP || e?.action == MotionEvent.ACTION_CANCEL) {
            setIsTemporaryOpenScroll(true)
        }
        if(!mIsOpenScroll || !mIsTemporaryOpenScroll){
            mNestedScrollInProgress = false
            mEnableOverScrollDrag = false
        }
        return super.dispatchTouchEvent(e)
    }

    override fun isEnableLoadMore(): Boolean {
        if(!mIsOpenScroll || !mIsTemporaryOpenScroll){
            return false
        }
        return super.isEnableLoadMore()
    }

    override fun isEnableRefresh(): Boolean {
        if(!mIsOpenScroll || !mIsTemporaryOpenScroll){
            return false
        }
        return super.isEnableRefresh()
    }


}

